package task

import (
	"github.com/robfig/cron/v3"
	"log"
)

type Status struct {
	Time   string `json:"time"`
	Status string `json:"status"`
}

var TaskStatus Status

var c *cron.Cron

func Init() {
	c = cron.New(cron.WithSeconds())

}

// Start 开启定时任务
func Start() error {
	_, err := c.AddFunc("@every "+TaskStatus.Time+"m", ExecuteTask)
	if err != nil {
		log.Println("定时任务启动失败：", err)
		return err

	}
	c.Start()
	TaskStatus.Status = "start"
	log.Println("定时任务已开启")
	return nil
}

// Stop 关闭定时任务
func Stop() {
	c.Stop()
	TaskStatus.Status = "stop"
	log.Println("定时任务已关闭")
}
